import Vue from 'vue';
import Vuex from 'vuex';
import createPersistedState from 'vuex-persistedstate';
import user from './modules/user.js';
import shopcart from './modules/shopcart.js'
import order from './modules/order.js'

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    onHomePage: true,
    default: 0,
    list: [
      {
        name: '雪儿',
        tell: '123455666',
        city: '河南省-开封市-龙亭区',
        address: '河南大学'
      },
      {
        name: '晴儿',
        tell: '123456789',
        city: '河南省-开封市-龙亭区',
        address: '河南大学'
      }
    ],
    messages: [
      { content: '这款车非常的好，便宜且实惠！！！', time: '2023-08-13 17:05:30' },
      { content: '这次体验非常好，会推荐！！！', time: '2023-08-14 18:56:42' }
    ]
  },
  getters: {
  },
  mutations: {
    setDefault(state, index) {
      state.default = index;
    },
    del(state, index) {
      state.list.splice(index, 1);
      // 判断是否删除默认值
      if (state.default === index) {
        state.default = 0;
      } else if (state.default > index) {
        state.default -= 1;
      }
    },
    add(state, item) {
      state.list.push(item)
    },
    edit(state, { index, item }) {
      state.list.splice(index, 1, item)
    },
    addCue (state, tem) {
      state.messages.push({
        content: tem.content,
        time: tem.time
      })
    },
    delCue(state, index) {
      state.messages.splice(index, 1)
    }
  },
  actions: {
  },
  modules: {
     shopcart,
     user,
     order
  },
  plugins: [createPersistedState()]
});
